//
//  UIActionSheet+MKBlockAdditions.m
//  UIKitCategoryAdditions
//
//  Created by Mugunth on 21/03/11.
//  Copyright 2011 Steinlogic All rights reserved.
//

#import "UIActionSheet+Block.h"
#import <objc/runtime.h>

@implementation UIActionSheet (Block)

static id kUIActionSheet_BBExtensionsCompletionBlockKey;

#pragma mark UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet*)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    void (^completion)(UIActionSheet* actionSheet, NSInteger buttonIndex);
    completion = objc_getAssociatedObject(self, &kUIActionSheet_BBExtensionsCompletionBlockKey);
    
    completion(actionSheet, buttonIndex);
}


#pragma mark Interface

- (void)setCompletion:(void (^)(UIActionSheet* actionSheet, NSInteger buttonIndex))completion
{
    if (completion == nil) return;
    
    self.delegate = self;
    objc_setAssociatedObject(self, &kUIActionSheet_BBExtensionsCompletionBlockKey,
                             completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end
